<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Admin\Controller\AdminController;
class WebController extends AdminController {

    function _initialize() {
        parent::_initialize();
        $this->assign('sys', 'website'); // 数据集
    }
    
    
    //推荐位管理
    public function index() {
        $position = M('recommend_position')->order('position_order')->select(); //推荐位
        $this->assign('position', $position);
        $position_id = I('get.position_id');
        if(empty($position_id)){
            $position_id = 1;
        }
        //选择推荐位后执行
        $this->assign('position_id',$position_id);
        $recommend = M('recommend')->where(array('recommend_type' => 2, 'position_id' =>$position_id))->order('recommend_id desc')->select();
        foreach ($recommend as $key => $value) {
            $recommend[$key]['goods'] = M('goods')->where("goods_id=".$value['goods_id'])->field('goods_id,goods_title,goods_thumb')->find();
        }
        $this->assign('recommend',$recommend);
        $this->assign('action', 'web');
        $this->display();
    }
    
    
    //保存数据
    public function addGoods(){
        $goods_id = I('goods_id');
        $position_id = I('position_id');
        //先查询是否已经添加过
        $where['goods_id'] = $goods_id;
        $where['position_id'] = $position_id;
        $recom = M('recommend')->where($where)->find();
        if($recom){
            $a['status'] = FALSE;
            $a['msg'] = "商品已存在，请选择其它商品！";
        }else{
            $data['position_id'] = $position_id;
            $data['goods_id'] = $goods_id;
            $re = M('recommend')->add($data);
            if($re){
                $goods = M('goods')->field('goods_id,goods_title,goods_thumb')->where('goods_id='.$goods_id)->find();
                $a['status'] = TRUE;
                $a['id'] = $goods['goods_id'];
                $a['rid'] = $re;
                $a['title'] = $goods['goods_title'];
                $a['image'] = $goods['goods_thumb'];
            }else{
                $a['status'] = FALSE;
                $a['msg'] = "系统错误，请稍候……";
            }
        }
        $this->ajaxReturn($a);
    }
    
    //删除数据
    public function delGoods(){
        $recommend_id = I('recommend_id');
        $re = M('recommend')->where('recommend_id='.$recommend_id)->delete();
        if($re){
            $a['status'] = TRUE;
        }else{
            $a['status'] = FALSE;
            $a['msg'] = "系统错误，请稍候……";
        }
        $this->ajaxReturn($a);
    }
    
}